<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="客户姓名">
          <el-input
            v-model="queryParams.applyName"
            placeholder="请输入客户姓名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="产品名称">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <!-- 产品类别 -->
        <el-form-item label="产品类别" >
          <el-select
              style="width: 200px"
              clearable
              v-model="queryParams.productCategorySelected"
              placeholder="请选择产品类别"
              @change="handleProductCategory"
              value-key="valueKey"
          >
            <el-option
                v-for="(item, index) in deviceTypeArrForIntelligent"
                :key="item.valueKey"
                :label="item.typeName"
                :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="改造类型">
          <el-select
            style="width: 150px"
            clearable
            v-model="queryParams.typeId"
            placeholder="请选择改造类型"
            @change="handleTypeChange"
          >
            <el-option
              v-for="item in change_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search"
@click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="warning" plain @click="handleExport" icon="Download"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="postList"
        stripe
        border
        max-height="500"
      >
        <el-table-column label="客户姓名" align="center" prop="applyName" />
        <el-table-column label="身份证号" align="center" prop="idCard" />
        <el-table-column label="改造类型" align="center">
          <template #default="scope">
            <span>
              {{
                change_type.find((item) => item.value == scope.row.typeId)
                  ? change_type.find((item) => item.value == scope.row.typeId)
                      .label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="产品类别"
          align="center"
          prop="productCategory"
        />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="单价（元）" align="center" prop="unitPrice" />

        <el-table-column label="数量" align="center" prop="number" />
        <el-table-column label="总价（元）" align="center" prop="totalPrice" />
        <el-table-column label="设备编号" align="center" prop="imei" />

        <!--        <el-table-column-->
        <!--            label="操作"-->
        <!--            align="center"-->
        <!--            class-name="small-padding fixed-width" width="220">-->
        <!--          <template #default="scope">-->
        <!--            <el-button link type="primary"  @click="edit(scope.row)" >解除绑定</el-button>-->
        <!--            <el-button link type="primary" @click="handinfo(scope.row)">视频通话</el-button>-->

        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.limit"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="elderlyReformExpense">
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex, product_category, change_type } =
  proxy.useDict(
    "sys_normal_disable",
    "sys_user_sex",
    "product_category",
    "change_type",
  );

import { elderOrientedRemodelFee } from "@/api/Expense/index";
import { getDeviceTypeArr } from "@/api/suitElderlyProduct";
const deviceTypeArrForIntelligent = ref([])
const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const isDisable = ref(true);
// 是否为智能化改造
const isIntelligentChange = ref(false);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    productName: undefined,
    applyName: undefined,
    typeId: undefined,
    productCategorySelected: undefined,
    productCategoryId: undefined,
    deviceCategoryId: undefined,
  },
});

const { queryParams, form } = toRefs(data);

/**
 * 修改产品类别
 * @param e
 */
const handleProductCategory = (item) => {

  console.log("【 item 】 = %o", item)

  queryParams.productCategorySelected = item.valueKey

  if(item.isDict) { // 业务字典
    queryParams.value.productCategoryId = item.id
    queryParams.value.deviceCategoryId = null
  } else { // 非业务字典
    queryParams.value.deviceCategoryId = item.id
    queryParams.value.productCategoryId = null
  }
}
const handleTypeChange = (e) => {
  /*if (e === '1') {
    // 给智能化改造数据赋值为真
    isIntelligentChange.value = true
    // 设备类别要清空
    queryParams.value.deviceCategoryId = queryParams.value.productCategoryId
    // queryParams.value.productCategoryId = "";
    // 选中智能化改造，发请求拿到设备类型数组
  } else {
    isDisable.value = false
    isIntelligentChange.value = false
    // queryParams.value.deviceCategoryId = '';
  }*/
};
function getDeviceTypeArrData() {
  getDeviceTypeArr()
    .then((res) => {
      deviceTypeArrForIntelligent.value = res.deviceTypeList

      for (const i in product_category.value) {
        const obj = {
          typeName: product_category.value[i].label,
          id: product_category.value[i].value,
          isDict: true
        }
        deviceTypeArrForIntelligent.value.unshift(obj)
      }

     deviceTypeArrForIntelligent.value.forEach((item) => {
          console.log("【 item 】 = %o", item)
          item.valueKey = item.id + item.typeName
      })

      console.log("【 result 】 = %o", deviceTypeArrForIntelligent.value);

    })
    .catch((error) => {})
}
/** 提交按钮 */

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  if (queryParams.value.typeId == "1") {
    queryParams.value.productCategoryId = "";
  } else {
    queryParams.value.deviceCategoryId = '';
  }
  elderOrientedRemodelFee(queryParams.value).then((response) => {
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  })
}
function handleExport() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以要加1，并且保证两位数格式
  const day = String(now.getDate()).padStart(2, '0'); // 日期保证两位数格式

  const currentDate = `${year}-${month}-${day}`;
  proxy.download(
    "pk-retirement//elder-oriented-remodel-fee/export",
    {
      ...queryParams.value,
    },
    `适老化改造费用${currentDate}.xlsx`,
  )
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    productName: undefined,
    applyName: undefined,
    typeId: undefined,
    productCategoryId: undefined,
    deviceCategoryId: undefined,
  };

  getList();
}

getList();
getDeviceTypeArrData()
</script>
